package com.sky.web.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //分类分页
    @GetMapping("/page")
    public Result findByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = categoryService.findByPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    //新增分类
    @PostMapping
    public Result insert(@RequestBody CategoryDTO categoryDTO) {
        categoryService.insert(categoryDTO);
        return Result.success();
    }

    //删除分类
    @DeleteMapping
    public Result deleteById(@RequestParam Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }

    // 根据类型查询分类
    @GetMapping("/list")
    public Result getList(Integer type) {
        List<Category> list = categoryService.getlist(type);
        return Result.success(list);
    }

    //根据id修改分类
    @PutMapping
    public Result updateById(@RequestBody Category category) {
        categoryService.updateById(category);
        return Result.success();
    }

    //分类启用/禁用
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable("status") Integer status,
                              @RequestParam Long id){
        categoryService.startOrStop(status,id);
        return Result.success();
    }
}
